package com.wmh.baseservice.system.controller;

import com.wmh.baseservice.common.utils.page.pojo.PageInfo;
import com.wmh.baseservice.common.utils.page.pojo.PageResult;
import com.wmh.baseservice.common.utils.page.util.PageUtil;
import com.wmh.baseservice.common.utils.returnbody.ResultCodeInfo;
import com.wmh.baseservice.common.utils.returnbody.ReturnBody;
import com.wmh.baseservice.system.pojo.SysMessage;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.wmh.baseservice.system.service.SysMessageService;

/**
 * @author mmx generator
 * @since 2021-08-26
 */
@Api(tags = "系统-消息管理")
@RestController
@RequestMapping(value = "system/sysMessage", produces = "text/plain;charset=utf-8")
@RequiresRoles("admin")
public class SysMessageController {

    @Resource
    private SysMessageService sysMessageService;
    @Resource
    private HttpServletRequest request;

    @ApiOperation("列表")
    @PostMapping("list")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataTypeClass = Long.class, name = "id", value = "id", required = false),
    })
    public String list(PageInfo pageInfo) {
        Long usId = (Long) request.getAttribute("usId");
        //执行分页辅助工具
        PageResult<SysMessage> pageResult = new PageUtil<SysMessage>(pageInfo).startPage((page, queryWrapper) -> {
            queryWrapper.eq(SysMessage.US_ID, usId);
            queryWrapper.select(SysMessage.TITLE, SysMessage.CONTENT, SysMessage.STATUS, SysMessage.ID, SysMessage.US_ID,SysMessage.CREATE_TIME);
            sysMessageService.page(page, queryWrapper);
        });
        return ReturnBody.success(pageResult);
    }


    @ApiOperation("已读")
    @PostMapping("read")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataTypeClass = Long.class, name = "id", value = "id", required = false),
    })
    public String read(Long id) {
        SysMessage sysMessage = sysMessageService.getByIdForUpdate(id);
        sysMessage.setStatus(1);
        if (!sysMessageService.updateById(sysMessage)) {
            return ReturnBody.error(ResultCodeInfo.SERVICE_EXCEPTION);
        }
        return ReturnBody.success();
    }

}
